#!/usr/bin/env python
#
#   Copyright (C) 2006 Jonathan Saggau                                    
#   saggau@gmail.com                                                      
#                                                                         
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                   
#                                                                         
#   This program is distributed in the hope that it will be useful,       
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                         
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                       
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#
import deftxt

def synthName(head, channels, overlap, synthType, envType):
    """generate the synthdef name from the unique attributes of a synthdef"""
    if channels == 1:
        chanStr = "Mono"
    else:
        chanStr = "Ster"
    newSynthDefname = ''.join((head, 
                               chanStr,
                               str(overlap), 
                               str(synthType)[:4], 
                               str(envType)[:4] 
                              ))
    return newSynthDefname

class Model(object):
    """Defaults, setters, and synthname generator for trigger controlling
       superclasses."""
    #outBus=0, bufnum=0, in=1, thresh = 0.004, amp = 1, duration =0.2, 
    #triggerID = 1,  envCurve = -7, attackDuration = 0.05, pan=0,  
    #bufferPosition =200;
    input = 1 #
    triggerID = 1000 # ?
    envCurve = -7 # ?
    pan = 0 # -1 ... 1
    volume = 1 #
    sampleDuration = 2 # (seconds) how much of buffer to play)
    sensitivity = 0.004 # ?
    bufferPosition = 200 # Frames
    attackSharpness = 0.05 # maps to attackDuration 
    overlap = 12
    channels = 2
    envType = u'percussive' #str from list
    envTypes = deftxt.envTypes
    synthType = u'static'
    synthTypes = deftxt.synthTypes
    output = 0
    
    _head = 'JASTrig'
    
    def __init__(self, nodeID, bufferID):
        self.nodeID = nodeID
        self.bufferID = bufferID
    
    def _synthargs(self):
        """Generates the args dict to load the synth and/or to generate
           the synthdef text override this in subclasses as needed."""
        if self.channels == 2:
            args = deftxt.args[2]
        else:
            args = deftxt.args[1]
            args['pan'] = self.pan
        args['in'] = self.input
        args['triggerID'] = self.triggerID
        args['envCurve'] = self.envCurve
        args['amp'] = self.volume
        args['duration'] = self.sampleDuration
        args['thresh'] = self.sensitivity
        args['bufferPosition'] = self.bufferPosition
        args['attackDuration'] = self.attackSharpness
        args['outBus'] = self.output
        args['bufnum'] = self.bufferID
        print args
        return args
    
    def synthName(self):
        return synthName(self._head, self.channels, self.overlap, 
                         self.synthType, self.envType)
                         
    def setNodeID(self, nodeID): 
        self.nodeID = nodeID 
        
    def setBufferID(self, bufID): 
        self.bufferID = bufID

    def setInput(self, input):
        self.input = input

    def setTriggerID(self, trigID):
        self.triggerID = trigID

    def setEnvCurve(self, curve):
        self.envCurve = curve

    def setPan(self, pan): # from -1 to 1
        self.pan = pan

    def setVolume(self, vol):
        self.volume = vol

    def setSampleDuration(self, duration):
        self.sampleDuration = duration

    def setSensitivity(self, sensitivity):
        self.sensitivity = sensitivity

    def setBufferPosition(self, position): #(in frames)
        self.bufferPosition = position

    def setAttackSharpness(self, sharpness): 
        self.attackSharpness = sharpness
        
    def setOut(self, out):
        self.out = out

    def setOverlap(self, overlap):
        self.overlap = overlap
        
    def setChannels(self, channels):
        self.channels = channels

    def setEnvType(self, envType):
        if envType in self.envTypes:
            self.envType = envType
        else:
            raise ValueError, 'Envelope type not valid: %s' %(envType,)

    def setSynthType(self, synthType):
        if synthType in self.synthTypes:
            self.synthType = synthType
        else:
            raise ValueError, 'Synth type not valid: %s' %(synthType,)